package admin

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"jwgin/model/mysql"
	"jwgin/service"
)

//前端后台路由

type route struct {
}

func (l *route) Routers(Router *gin.RouterGroup) (R gin.IRoutes) {
	Router.GET("route", l.get)
	return R
}

//获取路由
func (route) get(c *gin.Context) {

	RoleID, bools := c.Get("RoleID")
	if !bools {
		RespErr(c, fmt.Errorf("操作异常"))
		return
	}

	Account := c.GetString("RoleAccount")

	var (
		sql  string
		sql2 string
	)

	ret := []mysql.PermissionWeb{}
	if Account == "jwcode" {
		sql = fmt.Sprintf("web_pid = 0 ")
		sql2 = fmt.Sprintf("web_pid = ? ")
	} else {
		role := &mysql.Roles{}
		service.G.Where("rule_id = ?", RoleID).Find(role)
		sql = fmt.Sprintf("web_pid = 0 AND web_id in (%v)", role.RoleWebList)
		sql2 = fmt.Sprintf("web_pid = ? AND web_id in (%v)", role.RoleWebList)
	}

	service.G.Model(mysql.PermissionWeb{}).Where(sql).Find(&ret)
	for k, v := range ret {
		item := []mysql.PermissionWeb{}
		service.G.Model(mysql.PermissionWeb{}).Where(sql2, v.WebId).Find(&item)
		ret[k].PermissionWeb = item
	}
	RespSucc(c, ret)
}
